package com.otitan.forest.right.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.otitan.forest.right.entity.SysRoleEntity;
import com.otitan.forest.right.service.SysRoleService;
import com.otitan.webapp.framework.basepro.model.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 角色
 *
 * @author xiazw
 * @email *****@mail.com
 * @date 2020-08-04 14:19:44
 */
@Api(tags = "角色API")
@RestController
@RequestMapping("/sysRole")
@RequiresRoles(value = {"admin"}, logical = Logical.OR)
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;


    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public DataResult add(@RequestBody SysRoleEntity sysRole) {
        sysRoleService.checkInsertRole(sysRole);
        sysRoleService.save(sysRole);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete")
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sysRoleService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    public DataResult update(@RequestBody SysRoleEntity sysRole) {
        sysRoleService.checkUpdateRole(sysRole);
        sysRoleService.updateById(sysRole);
        return DataResult.success();
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "查询分页数据")
    @PostMapping("/listByPage")
    public DataResult findListByPage(@RequestBody SysRoleEntity sysRole) {
        Page page = new Page(sysRole.getPage(), sysRole.getSize());
        LambdaQueryWrapper<SysRoleEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(SysRoleEntity::getId, sysRole.getId());
        IPage<SysRoleEntity> iPage = sysRoleService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "根据Id查询实体")
    @GetMapping("/findOne/{id}")
    public DataResult findOneById(@PathVariable String id) {
        SysRoleEntity entityObj = sysRoleService.getById(id);
        return DataResult.success(entityObj);
    }

}
